<?php
namespace Pingxx\Widget;

use Think\Controller;


class PingxxWidget extends Controller
{


    public function getParams($order_id){
        $order = D('PingxxOrder')->getOrder($order_id);
        $aSubject = I('post.subject',L('_RECHARGE_SUBJECT_'));
        $aBody = I('post.body',  L('_RECHARGE_BODY_',array('channel'=>get_pay_method($order['method']))));
        if($order['payok']){
            $this->error(L('_ORDER_PAID_ERROR_'));
        }

        if($order['uid'] != is_login()){
            $this->error(L('_IS_NOT_YOUR_ORDER_'));
        }

        return array('channel'=>$order['method'],'body'=>$aBody,'subject'=>$aSubject,'amount'=>$order['amount']);
    }



    public function afterPay($event)
    {
        $order_id = $event['data']['object']['order_no'];
        $charge_id = $event['data']['object']['id'];
        $OrderModel = D('Pingxx/PingxxOrder');
        $OrderModel->setPayOk($order_id);
        $OrderModel->setChargeId($order_id, $charge_id);

        S('pingxx_charge_' . $charge_id, null);
        S('pingxx_order_' . $order_id, null);

        $order = $OrderModel->getOrder($order_id);

        $rechargeType = $order['recharge_type'];
        if (!$order['recharge_type']) {
            exit('error');
        }
        $ratio = $rechargeType['UNIT'];
        $step = floor($order['amount'] * $ratio);
        //设置积分
        D('Ucenter/Score')->setUserScore($order['uid'], $step, $order['field'], 'inc', 'pingxx_order', $order_id, L('_SOMEBODY_RECHARGE_', array('user' => get_nickname($order['uid']))));

    }


    public function render($order_id, $ext=array()){
        $link = M('order_link')->where(array('order_id'=>$order_id))->cache(60)->find();
        $order = M($link['model'])->where(array('id'=>$order_id))->find();

        $method = modC('METHOD', 'alipay_pc_direct', 'Pingxx');
        $this->assign('method', explode(',', $method));

        $amount = R(ucfirst($link['app']) . '/Pingxx/getAmount', array('order' => $order), 'Widget');  //获取付款总额。

        $this->assign('amount',$amount);
        $this->assign('ext',$ext);
        $this->assign('order_id',$order_id);
        $this->display(T('Application://Pingxx@Widget/pingxx'));
    }




}
